package com.qidianit.mes.pro.controller;

import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.ProductionOrder;
import com.qidianit.mes.pro.service.ProductionOrderService;
import com.qidianit.mes.pro.vo.ProductionOrderMaterialWithUnit;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Description:
 * @Author: shanzhe
 * @Date: 2025/9/20 - 14:37
 */
@RestController
@RequestMapping("/productionOrder")
public class ProductionOrderController {
    @Autowired
    private ProductionOrderService productionOrderService;

    @GetMapping("/list")
    public Result<PageInfo<ProductionOrderMaterialWithUnit>> queryProductionOrderListWithMultiConditionsAndPage(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,
                                                                                @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                                                                @RequestParam(value = "productionOrderCode",required = false) String productionOrderCode,
                                                                                @RequestParam(value = "productionOrderName",required = false) String productionOrderName,
                                                                                @RequestParam(value = "productionOrderSource",required = false) Integer productionOrderSource,
                                                                                @RequestParam(value = "materialCode",required = false) String materialCode,
                                                                                @RequestParam(value = "materialName",required = false) String materialName,
                                                                                @RequestParam(value = "customerCode",required = false) String customerCode,
                                                                                @RequestParam(value = "productionOrderType",required = false) Integer productionOrderType,
                                                                                @RequestParam(value = "requestDate",required = false) String requestDate,
                                                                                @RequestParam(value = "status",required = false) Integer status){
        return Result.success(productionOrderService.queryProductionOrderListWithMultiConditionsAndPage(pageNum, pageSize, productionOrderCode, productionOrderName, productionOrderSource, materialCode, materialName, customerCode, productionOrderType, requestDate,status));
    }

    @PostMapping("/add")
    public Result addProductionOrder(@RequestBody ProductionOrder productionOrder) {
        productionOrder.setCreateTime(LocalDateTime.now());
        productionOrder.setCreatorId(1L);
        return productionOrderService.addProduction(productionOrder) == 1?Result.success():Result.error("操作失败");
    }

    @GetMapping("/code/{code}")
    public Result queryProductionOrderCode(@PathVariable("code") String code) {
        return Result.success(productionOrderService.queryProductionOrderByCode(code));
    }

    @PutMapping("/update")
    public Result updateProductionOrder(@RequestBody ProductionOrder productionOrder) {
        productionOrder.setUpdateTime(LocalDateTime.now());
        productionOrder.setUpdatorId(1L);
        int result = productionOrderService.updateProductionOrder(productionOrder);
        return result == 1?Result.success():result == -1?Result.error("code重复"):Result.error("操作失败");
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteProductionOrder(@PathVariable("id") Long id) {
        return productionOrderService.deleteProductionOrderById(id) == 1?
                Result.success():Result.error("操作失败");
    }
}
